﻿using System;
using System.Web;
using System.Web.Mvc;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;

namespace CourseSystem.Stat.ActionFilter
{
    /// <summary>
    /// 学生、教师访问地址记录
    /// </summary>
    public class UrlStatFilter : FilterAttribute, IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //游客不做统计,专家不做统计
            if (UserHelper.LoginUser == null || UserHelper.LoginUser.UserType == UserTypeEnum.Guess || UserHelper.LoginUser.IsExpert)
                return;

            var ctrl = filterContext.RouteData.Values["Controller"].ToString().ToLower();
            var action = filterContext.RouteData.Values["Action"].ToString().ToLower();
            //保鲜
            if (ctrl == "stat" && action == "flash")
            {
                if (UserHelper.LoginUser.UserType == UserTypeEnum.Student)
                {
                    UrlStat.RemainStuFlash(UserHelper.LoginUser.Id);
                    return;
                }
                if (UserHelper.LoginUser.UserType == UserTypeEnum.Teacher)
                {
                    UrlStat.RemainTeaFlash(UserHelper.LoginUser.Id);
                    return;
                }
            }
            var id = filterContext.RouteData.Values["id"];
            if (id != null)
                id = "/" + id;
            var url = ctrl + "/" + action + (id ?? "") + HttpContext.Current.Request.Url.Query.ToLower();

            UrlStatModel info = null;
            if (UserHelper.LoginUser.UserType == UserTypeEnum.Student)
            {
                //学生地址初始化
                UrlMapping.InitUrl(ref url);
                //如果是指定的统计地址
                if (UrlMapping.StudentStatUrl.ContainsKey(url))
                {
                    //更新字典统计信息
                    UrlStat.UpdateStuUrlStat(UserHelper.LoginUser.Id, url);
                    return;
                }
                if (UrlStat.StudentUrlStatDic.TryGetValue(UserHelper.LoginUser.Id, out info))
                {
                    UrlStat.UrlChangeSubmit(info, true);
                }
                return;
            }

            if (UserHelper.LoginUser.UserType == UserTypeEnum.Teacher && !UserHelper.LoginUser.IsExpert)
            {
                //如果是指定的统计地址
                if (UrlMapping.TeacherStatUrl.ContainsKey(url))
                {
                    //更新字典统计信息
                    UrlStat.UpdateTeaUrlStat(UserHelper.LoginUser.Id, url);
                    return;
                }
                if (UrlStat.TeacherUrlStatDic.TryGetValue(UserHelper.LoginUser.Id, out info))
                {
                    UrlStat.UrlChangeSubmit(info, true);
                }
                return;
            }

        }



        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            return;
        }
    }
}
